package com.neusoft.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.neusoft.entity.Book;
import com.neusoft.entity.BookClass;
import com.neusoft.entity.common.PageBookClass;
import com.neusoft.entity.common.PageBookName;
import com.neusoft.entity.model.BookByClass;
import com.neusoft.entity.model.BookCustom;
import com.neusoft.entity.model.UserBorrowInfo;
import com.neusoft.mapper.modelmapper.BookInvMapper;
import com.neusoft.mapper.modelmapper.UserBorrowInfoMapper;
import com.neusoft.service.BookClassService;
import com.neusoft.service.BookService;
import com.neusoft.service.BorrowInfoService;

@Controller
@RequestMapping("/{from}")
public class BookController {
	@Autowired
	BookInvMapper bookInvMapper;

	@Autowired
	BookService bookService;

	@Autowired
	BorrowInfoService borrowInfoService;

	@Autowired
	BookClassService bookClassService;

	@Autowired
	UserBorrowInfoMapper userBorrowInfoMapper;

	// ///////////////////////////////////
	@RequestMapping("/selectByBookName")
	public ModelAndView selectByBookName(String bookName, int pageNum, @PathVariable("from") String from) {
		ModelAndView modelAndView = new ModelAndView();
		if (from.equals("admin")) {
			modelAndView.setViewName("admin/books");
		} else if (from.equals("operator")) {
			modelAndView.setViewName("operator/books");
		} else {
			modelAndView.setViewName("user/books");
		}
		int startIndex = (pageNum - 1) * 10;
		int endIndex = startIndex + 10;
		PageBookName pageBookName = new PageBookName(bookName, startIndex, endIndex);
		List<BookCustom> bookCustom;
		try {
			bookCustom = bookInvMapper.findBookCustomsByBName(pageBookName);
			modelAndView.addObject("bookCustom", bookCustom);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return modelAndView;
	}

	@RequestMapping("/getAllClass")
	public ModelAndView getAllClass(@PathVariable("from") String from) {
		ModelAndView modelAndView = new ModelAndView();
		if (from.equals("admin")) {
			modelAndView.setViewName("admin/books");
		} else if (from.equals("operator")) {
			modelAndView.setViewName("operator/books");
		} else {
			modelAndView.setViewName("user/books");
		}
		List<BookClass> bookClassList = bookClassService.loadListByExample(null);// //////////////////////
		modelAndView.addObject("bookClssList", bookClassList);
		return modelAndView;
	}

	@RequestMapping("/selectByClassName")
	public ModelAndView selectByClassName(@PathVariable("from") String from, int pageNum, String className) {
		ModelAndView modelAndView = new ModelAndView();
		if (from.equals("admin")) {
			modelAndView.setViewName("admin/books");
		} else if (from.equals("operator")) {
			modelAndView.setViewName("operator/books");
		} else {
			modelAndView.setViewName("user/books");
		}
		///
		int startIndex = (pageNum - 1) * 10;
		int endIndex = startIndex + 10;
		PageBookClass pageBookClass = new PageBookClass(className, startIndex, endIndex);

		List<BookByClass> bookList;
		try {
			bookList = bookInvMapper.findBookByClass(pageBookClass);
			modelAndView.addObject("bookList", bookList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// ////////////////////////////////////////////////////

		return modelAndView;
	}

	@RequestMapping("/addBook")
	public ModelAndView addBook(Book book, @PathVariable("from") String from) {
		ModelAndView modelAndView = new ModelAndView();
		if (from.equals("admin")) {
			modelAndView.setViewName("admin/books");
			bookService.addBookSelective(book);
			modelAndView.addObject("addbook", book); ////////////////////////////////////
			return modelAndView;
		}
		return null;
	}

	@RequestMapping("/addClass")
	public ModelAndView addClass(String className, @PathVariable("from") String from) {
		if (from.equals("admin")) {
			BookClass record = new BookClass();
			record.setBookClass(className);
			bookClassService.insertSelective(record);
			// ////////////////////////////////////////增加分类
			return getAllClass(from);
		}
		return null;
	}

	@RequestMapping("/getBorrowRecord")
	public ModelAndView getBorrowRecord(int userId, @PathVariable("from") String from) {
		ModelAndView modelAndView = new ModelAndView();
		if (from.equals("operator")) {
			modelAndView.setViewName("operator/borrows");
		} //////////////////////////////////////////////// 要else吗？

		UserBorrowInfo borrowInfosList = null;
		;
		try {
			borrowInfosList = userBorrowInfoMapper.findUserBorrowInfoByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		modelAndView.addObject("borrowInfosList", borrowInfosList);
		return modelAndView;
	}

	@RequestMapping("/returnBook")
	public ModelAndView returnBook(Integer bookId, @PathVariable("from") String from) {
		ModelAndView modelAndView = new ModelAndView();
		if (from.equals("operator")) {
			modelAndView.setViewName("operator/bowrrows");
		} //////////////////////////////////////////////// 要else吗？返回值1或0对吗
		if (borrowInfoService.removeById(bookId) == 1) {
			modelAndView.addObject("successMess", "删除成功！"); //////// gaiwei mess
		} else {
			modelAndView.addObject("errorMess", "删除失败！"); ///////////
		}
		return modelAndView;
	}
}
